#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Index.h>
#pragma unmanaged 
#include <ql\indexes\interestrateindex.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL;
using namespace Cephei::QL::Times;
#define HANDLE
#define ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Indexes {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IInterestRateIndex
	public ref class CInterestRateIndex : 
            public CIndex,
            public Cephei::QL::Indexes::IInterestRateIndex
	{
	protected: 
		boost::shared_ptr<QuantLib::InterestRateIndex>* _ppInterestRateIndex;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::InterestRateIndex>* _phInterestRateIndex;
#endif
		Object^ _InterestRateIndexOwner;     // reference to object that manages the storage for this object
	internal:
        CInterestRateIndex (boost::shared_ptr<QuantLib::InterestRateIndex>& childNative, Object^ owner);
        CInterestRateIndex (QuantLib::InterestRateIndex& childNative, Object^ owner);
        CInterestRateIndex (CInterestRateIndex^ copy);
        CInterestRateIndex (System::Type^ t);
#ifdef STRUCT
        CInterestRateIndex (QuantLib::InterestRateIndex childNative);
#endif       
#ifdef HANDLE
		CInterestRateIndex (QuantLib::Handle<QuantLib::InterestRateIndex>& childNative, Object^ owner);
		CInterestRateIndex (QuantLib::Handle<QuantLib::InterestRateIndex> childNative);
#endif
		virtual ~CInterestRateIndex ();
		!CInterestRateIndex ();

	internal:
		QuantLib::InterestRateIndex& GetReference ();
		boost::shared_ptr<QuantLib::InterestRateIndex>& GetShared ();
		QuantLib::InterestRateIndex* GetPointer ();
        void SetInterestRateIndex (boost::shared_ptr<QuantLib::InterestRateIndex> native)
        {
            if (_ppInterestRateIndex != NULL)
                delete _ppInterestRateIndex;
            _ppInterestRateIndex = new boost::shared_ptr<QuantLib::InterestRateIndex> (native);
            SetIndex (boost::dynamic_pointer_cast<QuantLib::Index> (*_ppInterestRateIndex));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::InterestRateIndex>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
        property Cephei::QL::ICurrency^ Currency 
        {
		    virtual Cephei::QL::ICurrency^ get () ;
        }
        property Cephei::QL::Times::IDayCounter^ DayCounter 
        {
		    virtual Cephei::QL::Times::IDayCounter^ get () ;
        }
        property String^ FamilyName 
        {
		    virtual String^ get () ;
        }
		virtual Double Fixing (DateTime fixingDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ forecastTodaysFixing) ;
        property Cephei::QL::Times::ICalendar^ FixingCalendar 
        {
		    virtual Cephei::QL::Times::ICalendar^ get () ;
        }
		virtual DateTime FixingDate (DateTime valueDate) ;
        property UInt32 FixingDays 
        {
		    virtual UInt32 get () ;
        }
		virtual Boolean IsValidFixingDate (DateTime fixingDate) ;
		virtual DateTime MaturityDate (DateTime valueDate) ;
        property String^ Name 
        {
		    virtual String^ get () ;
        }
        property Cephei::QL::Times::IPeriod^ Tenor 
        {
		    virtual Cephei::QL::Times::IPeriod^ get () ;
        }
        property Cephei::QL::Indexes::IInterestRateIndex^ Update 
        {
		    virtual Cephei::QL::Indexes::IInterestRateIndex^ get () ;
        }
		virtual DateTime ValueDate (DateTime fixingDate) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IInterestRateIndex Vector interface
	ref class CInterestRateIndexVector : public System::MarshalByRefObject,  
							    public INativeVector<IInterestRateIndex^>,
							    public INativeVector<Generic::ICell<IInterestRateIndex^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::InterestRateIndex>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::InterestRateIndex>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::InterestRateIndex>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CInterestRateIndexVector ();
#ifdef STRUCT
		CInterestRateIndexVector (std::vector<QuantLib::InterestRateIndex>& vec, Object^ owner);
		CInterestRateIndexVector (std::vector<QuantLib::InterestRateIndex> vec);
#endif       
		CInterestRateIndexVector (std::vector<boost::shared_ptr<QuantLib::InterestRateIndex>>& vec, Object^ owner);
#ifdef HANDLE
		CInterestRateIndexVector (std::vector<QuantLib::Handle<QuantLib::InterestRateIndex>>& vec, Object^ owner);
#endif
		virtual ~CInterestRateIndexVector ();
		!CInterestRateIndexVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IInterestRateIndex^ default[int] 
		{
			virtual IInterestRateIndex^ get (int index) = INativeVector<IInterestRateIndex^>::default::get;
			virtual void set (int index, IInterestRateIndex^ value) = INativeVector<IInterestRateIndex^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Generic::ICell<Cephei::QL::Indexes::IInterestRateIndex^>^ CItem[int] 
		{
			virtual Generic::ICell<IInterestRateIndex^>^ get (int index) = INativeVector<Generic::ICell<IInterestRateIndex^>^>::default::get;
			virtual void set (int index, Generic::ICell<IInterestRateIndex^>^ value) = INativeVector<Generic::ICell<IInterestRateIndex^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::InterestRateIndex>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::InterestRateIndex>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::InterestRateIndex>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CInterestRateIndex_Factory : public System::MarshalByRefObject,  public IInterestRateIndex_Factory
	{
	public:
        /* <summary> Create a hybrid Vector of IInterestRateIndex, with event notification of changes </summary> */
        virtual Cephei::IVector<IInterestRateIndex^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IInterestRateIndex, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IInterestRateIndex^>^>^>^ CreateCellVector();
        virtual Cephei::IVector<IInterestRateIndex^>^ CreateVector(System::Collections::Generic::IEnumerable<IInterestRateIndex^>^ source);
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IInterestRateIndex^>^>^>^ CreateCellVector(System::Collections::Generic::IEnumerable<Generic::ICell<IInterestRateIndex^>^>^ source);
    };
   
/*Cephei*/ } /*QL*/ } /*Indexes */}
